// 保存用户的信息
import ACCESS_ENUM from "@/access/ACCESS_ENUM";
import { log } from "console";
import { defineStore } from "pinia"; // 定义容器

export const userStore = defineStore("userStore", {
  /**
   * 存储全局状态
   * 1.必须是箭头函数: 为了在服务器端渲染的时候避免交叉请求导致数据状态污染
   * 和 TS 类型推导
   */
  //   存储用户状态信息
  state: () => {
    return {
      userAccount: "", //用户账号
      userName: "45456", //用户名
      userEmail: "", //用户邮箱
      userAvatar: "", //用户头像
      userRole: ACCESS_ENUM.ADMIN,
      loginState: false,
      acNumber: 0, //ac题目数量
    };
  },
  /**
   * 用来封装计算属性 有缓存功能  类似于computed
   */
  getters: {},
  /**
   * 编辑业务逻辑  类似于methods
   */
  actions: {
    setLoginUser(userRole: string) {
      this.userRole = userRole;
    },
    setUserInfo(userInfo: any) {
      const userData = userInfo;
      this.userAccount = userData?.userAccount ?? "undefined";
      this.userName = userData?.userName ?? "undefined";
      this.userEmail = userData?.userEmail ?? "undefined";
      this.userAvatar = userData?.userAvatar ?? "undefined";
      this.userRole = userData?.userRole ?? "undefined";
      this.acNumber = userData?.acNumber ?? "undefined";
      this.loginState = true;
    },
  },
});
